import jwt from 'jsonwebtoken';

interface TokenInter{
    token:string
}

interface verifyResInter{

}

//jwt密钥
const secretKey = 'chentianyu';

// const token = jwt.sign({ data: 'your_data' }, secretKey);

export default defineEventHandler(async(event:any)=>{
    const {token}:TokenInter = await readBody(event)
    // console.log(token)
    // 验证 JWT
    const verifyRes = jwt.verify(token, secretKey, (err:any, decoded:any):verifyResInter|null => {
        if (err) {
            // console.log("JWT验证:",err)
            return null
        } else {
            // console.log("JWT验证:",decoded)
            return decoded
            
        }
    });
    // console.log(verifyRes)
    if(verifyRes != null){
        //验证成功
        return{
            status: 200,
            data:{
                account:verifyRes.account,
                password: verifyRes.password
            },
            message:"request success"
        }
    }else{
        //验证失败
        
        return{
            status: 500,
            message:"request fail"
        }
    }
})

